package com.sam;


import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity{
	SocketService mBoundService; 
	boolean mIsBound = false;
	Button videoBtn;
	Button liveBtn;
	MainActivity mAct;
	String[] values;
	
	private ServiceConnection mConnection = new ServiceConnection() {
		// EDITED PART
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			mBoundService = ((SocketService.LocalBinder) service).getService();
			videoBtn.setEnabled(true);
			liveBtn.setEnabled(true);
			
			videoBtn.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					
					 mBoundService.getTCPClient().sendMessage(new DataStream("video",null,null));
		        	      

					
				}
			});
			
			liveBtn.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					Intent intent = new Intent(getBaseContext(),LiveView.class);
					startActivity(intent);

					
				}
			});
			
			
			

		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
			mBoundService = null;
		}

	};

	private void doBindService() {
		bindService(new Intent(this, SocketService.class), mConnection,
				Context.BIND_AUTO_CREATE);
		mIsBound = true;
		if (mBoundService != null) {
			mBoundService.IsBoundable();
		}
	}

	private void doUnbindService() {
		if (mIsBound) {
			// Detach our existing connection.
			unbindService(mConnection);
			mIsBound = false;
		}
	}


	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_page);
		videoBtn = (Button) findViewById(R.id.videoArch);
		mAct=this;
		liveBtn =  (Button) findViewById(R.id.liveSur);
		
		videoBtn.setEnabled(false);
		liveBtn.setEnabled(false);
		Bundle extras = getIntent().getExtras();
		values = new String[extras.getStringArray("movies").length];
		values = extras.getStringArray("movies");
		for(int i=0;i<extras.getStringArray("movies").length;i++)
			Log.i("MainActvitySessionId",values[i]);
		
		doBindService();
		
						
	}
	
	@Override
	public void onBackPressed() {
	this.moveTaskToBack(true);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		doUnbindService();
	}
}
